package com.hh.vietnameseproject.ui.person

import android.app.Activity
import android.app.ActivityOptions
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.transition.Slide
import android.view.Gravity
import android.view.Window
import com.hh.vietnameseproject.databinding.ActivityChangePlanBinding
import com.hh.vietnameseproject.databinding.ActivityPersonBinding
import com.hh.vietnameseproject.logic.network.AllNetWork
import com.hh.vietnameseproject.ui.MainActivity
import com.hh.vietnameseproject.ui.login.ActivityController
import com.hh.vietnameseproject.ui.login.LoginActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class PersonActivity : AppCompatActivity() {

    lateinit var binding: ActivityPersonBinding

    val addPwdDialog by lazy {
        AddPasswordDialog(this)
    }
    val changePwdDialog by lazy {
        ChangePasswordDialog(this)
    }
    val changeNameDialog by lazy {
        ChangeNameDialog(this)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityPersonBinding.inflate(layoutInflater)
        ActivityController.addList(this)
        window.requestFeature(Window.FEATURE_CONTENT_TRANSITIONS)
        val slide = Slide()
        slide.slideEdge = Gravity.RIGHT
        window.enterTransition = slide
        window.exitTransition = slide
        setContentView(binding.root)
        val id = intent.getStringExtra("id")
        val name = intent.getStringExtra("name")
        val bitmap = intent.getParcelableExtra<Bitmap>("bitmap")
        binding.personChangeNameTv.text = name
        binding.personChangeId.text = id
        binding.personHeadImage.setImageBitmap(bitmap)
        GlobalScope.launch {
            val body = AllNetWork.getHeadImage(id.toString())
            withContext(Dispatchers.Main) {
                binding.personHeadImage.setImageBitmap(BitmapFactory.decodeStream(body.byteStream()))
            }
        }
        binding.personAddPwd.setOnClickListener {
            addPwdDialog.show()
        }
        binding.personBack.setOnClickListener {
            onBackPressed()
        }
        binding.personChangePwd.setOnClickListener {
            changePwdDialog.show()
        }
        binding.personChangeNameLayout.setOnClickListener {
            changeNameDialog.show()
        }
        binding.personExitBtn.setOnClickListener {
            ActivityController.closeAll()
            startActivity(Intent(this,LoginActivity::class.java))
        }
    }
    fun changeName(name:String){
        binding.personChangeNameTv.text = name
        mainActivity.changeMeFragmentName(name)
    }

    companion object {
        lateinit var mainActivity: MainActivity
        fun actionStart(context: Context?, id: String, name: String) {
            mainActivity = context as MainActivity
            val intent = Intent(context, PersonActivity::class.java)
            intent.putExtra("id", id)
            intent.putExtra("name", name)
            context?.startActivity(
                intent,
                ActivityOptions.makeSceneTransitionAnimation(context as Activity).toBundle()
            )
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ActivityController.removeList(this)
    }
}